﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.EmphasizeContinuousMfg.CUiECMEmphasizeContinuousMfgDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui;
using UiInternalControls.WindowsFormsControls;


namespace DnProcessSimulateCommands.EmphasizeContinuousMfg;

public class CUiECMEmphasizeContinuousMfgDlg : TxForm
{
  private const string COLLECTION_NAME = "CUiECMEmphasizeContinuousMfgDlg";
  private const string LAST_SELECTED_WIDTH = "LastSelectedWidth";
  private const string LAST_SELECTED_COLOR = "LastSelectedColor";
  private const string DOES_HAVE_LAST_SELECTED_COLOR = "DoesHaveLastSelectedColor";
  private CUiECMSelectWidthDlg m_widthDlg;
  private CApECMEmphasizingModel m_model;
  private CApECMEmphasizingManager m_manager;
  private IContainer components;
  private TxToolStrip m_toolstrip;
  private TxColorSelectToolStripButton m_colorMenu;
  private Label label1;
  private TxToolStripDropDownButton m_widthMenu;
  private TxToolStripButton resetDefaultToolStripButton;

  public CUiECMEmphasizeContinuousMfgDlg()
  {
    this.InitializeComponent();
    this.m_widthMenu.SetImage(TxImageManager.GetImageByKey("EmphasizeWidth"));
    this.resetDefaultToolStripButton.SetImage(TxImageManager.GetImageByKey("Reset"));
    ((Control) this.m_toolstrip).Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
    this.Initialize();
    this.RegisterFormEvents();
    this.RegisterGeneralEvents();
    this.LoadDialogConfiguration();
    this.UpdateBucketSelectedColor();
  }

  private void Initialize()
  {
    this.m_model = CApECMEmphasizingModel.Instance;
    this.m_manager = CApECMEmphasizingManager.Instance;
    ((ToolStripDropDownItem) this.m_widthMenu).DropDownItems.AddRange(this.GenerateWidthMenuItems());
  }

  public virtual void OnInitTxForm() => base.OnInitTxForm();

  private DialogResult OpenSelectWidthDlg()
  {
    this.m_widthDlg = new CUiECMSelectWidthDlg((double) this.m_model.CurrentWidth, this);
    DialogResult dialogResult = ((Form) this.m_widthDlg).ShowDialog();
    this.m_model.CurrentWidth = Convert.ToInt32(this.m_widthDlg.SelectedWidth);
    this.m_widthDlg = (CUiECMSelectWidthDlg) null;
    return dialogResult;
  }

  private void CheckItemFromMenu(ToolStripMenuItem item, ToolStripDropDownItem root)
  {
    this.UncheckAllOptions(root);
    ToolStripMenuItem toolStripMenuItem = item;
    if (!root.DropDownItems.Contains((ToolStripItem) toolStripMenuItem) || !toolStripMenuItem.CheckOnClick)
      return;
    toolStripMenuItem.Checked = true;
  }

  private void UncheckAllOptions(ToolStripDropDownItem root)
  {
    foreach (ToolStripItem dropDownItem in (ArrangedElementCollection) root.DropDownItems)
    {
      if (dropDownItem is ToolStripMenuItem)
      {
        ToolStripMenuItem toolStripMenuItem = dropDownItem as ToolStripMenuItem;
        if (toolStripMenuItem.CheckOnClick)
          toolStripMenuItem.Checked = false;
      }
    }
  }

  private void UpdateBucketSelectedColor()
  {
    if (this.m_model.DoesHaveCurrentColor)
    {
      Color color = this.m_model.CurrentColor.Color;
      if (color.ToArgb() == ((ToolStripItem) this.m_colorMenu).ImageTransparentColor.ToArgb())
        color = color.B <= (byte) 0 ? Color.FromArgb((int) color.R, (int) color.G, (int) color.B + 1) : Color.FromArgb((int) color.R, (int) color.G, (int) color.B - 1);
      this.m_colorMenu.SelectedColor = color;
    }
    else
      this.m_colorMenu.SelectedColor = this.m_model.DefaultColor.Color;
  }

  private ToolStripItem[] GenerateWidthMenuItems()
  {
    int length = 7;
    ToolStripItem[] widthMenuItems = new ToolStripItem[length];
    for (int index = 0; index < length - 1; ++index)
    {
      ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
      string str = " " + ECMStringTable.PIXELS;
      if (index == 0)
        str = " " + ECMStringTable.PIXEL;
      toolStripMenuItem.Tag = (object) (index + 1);
      toolStripMenuItem.Text = (index + 1).ToString() + str;
      toolStripMenuItem.CheckOnClick = true;
      toolStripMenuItem.Click += new EventHandler(this.widthMenuItem_Click);
      widthMenuItems[index] = (ToolStripItem) toolStripMenuItem;
    }
    ToolStripMenuItem toolStripMenuItem1 = new ToolStripMenuItem();
    toolStripMenuItem1.CheckOnClick = true;
    toolStripMenuItem1.Tag = (object) 0;
    toolStripMenuItem1.Text = ECMStringTable.OTHER_MENU_TEXT;
    toolStripMenuItem1.Click += new EventHandler(this.selectWidthItem_Click);
    widthMenuItems[length - 1] = (ToolStripItem) toolStripMenuItem1;
    return widthMenuItems;
  }

  private void EmphasizeSelectedMfgsColor() => this.m_manager.EmphasizeSelectedMfgsColor();

  private void EmphasizeSelectedMfgsWidth() => this.m_manager.SetWidthToSelectedMfgs();

  private void EmphasizeAllMfgsWidth() => this.m_manager.SetWidthToAllMfgs(true);

  private void UnEmphasizeSelectedMfgsColor() => this.m_manager.UnEmphasizeSelectedMfgsColor();

  private void RegisterFormEvents()
  {
    ((Form) this).FormClosing += new FormClosingEventHandler(this.CUiECMEmphasizeContinuousMfgDlg_FormClosing);
    this.m_colorMenu.SelectedColorChanged += new EventHandler(this.m_colorMenu_SelectedColorChanged);
  }

  private void UnregisterFormEvents()
  {
    ((Form) this).FormClosing -= new FormClosingEventHandler(this.CUiECMEmphasizeContinuousMfgDlg_FormClosing);
    this.m_colorMenu.SelectedColorChanged -= new EventHandler(this.m_colorMenu_SelectedColorChanged);
  }

  private void RegisterGeneralEvents() => this.RegisterSelectionEvents();

  private void RegisterSelectionEvents()
  {
    TxApplication.ActiveSelection.ItemsSet += new TxSelection_ItemsSetEventHandler(this.ActiveSelection_ItemsSet);
    TxApplication.ActiveSelection.ItemsAdded += new TxSelection_ItemsAddedEventHandler(this.ActiveSelection_ItemsAdded);
    TxApplication.ActiveSelection.ItemsRemoved += new TxSelection_ItemsRemovedEventHandler(this.ActiveSelection_ItemsRemoved);
  }

  private void UnregisterGeneralEvents() => this.UnregisterSelectionEvents();

  private void UnregisterSelectionEvents()
  {
    TxApplication.ActiveSelection.ItemsSet -= new TxSelection_ItemsSetEventHandler(this.ActiveSelection_ItemsSet);
    TxApplication.ActiveSelection.ItemsAdded -= new TxSelection_ItemsAddedEventHandler(this.ActiveSelection_ItemsAdded);
    TxApplication.ActiveSelection.ItemsRemoved -= new TxSelection_ItemsRemovedEventHandler(this.ActiveSelection_ItemsRemoved);
  }

  private void SynchronizeUiWithSelectedMfgWidth()
  {
    int width = -1;
    if (this.m_manager.DoesAllSelectedMfgsHaveTheSameWidth(out width) && width > -1)
      this.CheckMenuItemWithCustomWidth(width);
    else
      this.UncheckAllOptions((ToolStripDropDownItem) this.m_widthMenu);
  }

  private void CheckMenuItemWithCustomWidth(int width)
  {
    ToolStripMenuItem toolStripMenuItem1 = (ToolStripMenuItem) null;
    ToolStripMenuItem toolStripMenuItem2 = (ToolStripMenuItem) null;
    foreach (ToolStripItem dropDownItem in (ArrangedElementCollection) ((ToolStripDropDownItem) this.m_widthMenu).DropDownItems)
    {
      if (dropDownItem is ToolStripMenuItem toolStripMenuItem3 && toolStripMenuItem3.Tag != null && toolStripMenuItem3.Tag is int)
      {
        if (Convert.ToInt32(toolStripMenuItem3.Tag) == width)
        {
          toolStripMenuItem1 = toolStripMenuItem3;
          break;
        }
        if (Convert.ToInt32(toolStripMenuItem3.Tag) == 0)
          toolStripMenuItem2 = toolStripMenuItem3;
      }
    }
    if (toolStripMenuItem1 == null && toolStripMenuItem2 != null)
      toolStripMenuItem1 = toolStripMenuItem2;
    if (toolStripMenuItem1 == null)
      return;
    this.CheckItemFromMenu(toolStripMenuItem1, (ToolStripDropDownItem) this.m_widthMenu);
    this.m_model.CurrentWidth = width;
  }

  private void SaveDialogConfiguration()
  {
    string str = nameof (CUiECMEmphasizeContinuousMfgDlg);
    TxFormSettings.StoreFormValue((Form) this, str, "LastSelectedWidth", this.m_model.CurrentWidth.ToString());
    uint uint32 = Convert.ToUInt32(this.m_model.CurrentColor.RGB);
    TxFormSettings.StoreFormValue((Form) this, str, "LastSelectedColor", uint32.ToString());
    TxFormSettings.StoreFormValue((Form) this, str, "DoesHaveLastSelectedColor", this.m_model.DoesHaveCurrentColor.ToString());
  }

  private void LoadDialogConfiguration()
  {
    this.LoadWidthConfiguration();
    this.LoadColorConfiguration();
  }

  private void LoadWidthConfiguration()
  {
    object obj;
    TxFormSettings.RestoreFormValue((Form) this, nameof (CUiECMEmphasizeContinuousMfgDlg), "LastSelectedWidth", ref obj);
    if (obj == null)
      return;
    int num = 0;
    try
    {
      num = Convert.ToInt32(obj);
    }
    catch
    {
    }
    if (num == 0)
      num = this.m_model.DefaultWidth;
    this.m_model.CurrentWidth = num;
    this.CheckMenuItemWithCustomWidth(this.m_model.CurrentWidth);
  }

  private void LoadColorConfiguration()
  {
    string str = nameof (CUiECMEmphasizeContinuousMfgDlg);
    object obj1;
    TxFormSettings.RestoreFormValue((Form) this, str, "LastSelectedColor", ref obj1);
    if (obj1 == null)
      return;
    uint num = 0;
    try
    {
      num = Convert.ToUInt32(obj1);
    }
    catch
    {
    }
    this.m_model.CurrentColor = new TxColor(num);
    object obj2;
    TxFormSettings.RestoreFormValue((Form) this, str, "DoesHaveLastSelectedColor", ref obj2);
    this.m_model.DoesHaveCurrentColor = Convert.ToBoolean(obj2);
    this.UpdateBucketSelectedColor();
  }

  private void m_colorMenu_Click(object sender, EventArgs e) => this.m_colorMenu.ShowPopup();

  private void m_colorMenu_SelectedColorChanged(object sender, EventArgs e)
  {
    Color selectedColor = this.m_colorMenu.SelectedColor;
    this.m_model.CurrentColor = new TxColor(selectedColor.R, selectedColor.G, selectedColor.B);
    this.m_model.DoesHaveCurrentColor = true;
    this.UpdateBucketSelectedColor();
    this.EmphasizeSelectedMfgsColor();
  }

  private void resetDefaultToolStripButton_Click(object sender, EventArgs e)
  {
    this.m_model.ResetToDefaultColor();
    if (this.m_model.DoesHaveCurrentColor)
      this.EmphasizeSelectedMfgsColor();
    else
      this.UnEmphasizeSelectedMfgsColor();
    this.UpdateBucketSelectedColor();
    this.m_model.ResetToDefaultWidth();
    this.CheckMenuItemWithCustomWidth(this.m_model.CurrentWidth);
    this.EmphasizeSelectedMfgsWidth();
  }

  private void selectWidthItem_Click(object sender, EventArgs e)
  {
    if (!(sender is ToolStripMenuItem toolStripMenuItem))
      return;
    bool flag = toolStripMenuItem.Checked;
    if (this.OpenSelectWidthDlg() != DialogResult.Cancel)
      this.CheckItemFromMenu(toolStripMenuItem, (ToolStripDropDownItem) this.m_widthMenu);
    else
      toolStripMenuItem.Checked = !flag;
  }

  private void widthMenuItem_Click(object sender, EventArgs e)
  {
    if (!(sender is ToolStripMenuItem toolStripMenuItem))
      return;
    this.CheckItemFromMenu(toolStripMenuItem, (ToolStripDropDownItem) this.m_widthMenu);
    if (toolStripMenuItem.Tag == null || !(toolStripMenuItem.Tag is int))
      return;
    this.m_model.CurrentWidth = Convert.ToInt32(toolStripMenuItem.Tag);
    this.EmphasizeSelectedMfgsWidth();
  }

  private void ActiveSelection_ItemsRemoved(object sender, TxSelection_ItemsRemovedEventArgs args)
  {
    this.SynchronizeUiWithSelectedMfgWidth();
  }

  private void ActiveSelection_ItemsAdded(object sender, TxSelection_ItemsAddedEventArgs args)
  {
    this.SynchronizeUiWithSelectedMfgWidth();
  }

  private void ActiveSelection_ItemsSet(object sender, TxSelection_ItemsSetEventArgs args)
  {
    this.SynchronizeUiWithSelectedMfgWidth();
  }

  private void CUiECMEmphasizeContinuousMfgDlg_FormClosing(object sender, FormClosingEventArgs e)
  {
    if (TxDoNotShowAgainMessageBox.Show(ECMStringTable.TOOLBOX_CLOSE_WARNING, ECMStringTable.EMPHASIZE_CONTINUOUS_MFG_NAME, "TX_EMPHASIZE_CONTINUOUS_MFG_CLOSE_TOOLBOX_CONFIRM") == DialogResult.Yes)
      this.PrepareClosingOfDialog();
    else
      e.Cancel = true;
  }

  public void EmphasizeSelectedMfgsWidth(int width)
  {
    this.m_model.CurrentWidth = width;
    this.EmphasizeSelectedMfgsWidth();
  }

  public void CloseDlg()
  {
    this.PrepareClosingOfDialog();
    ((Form) this).Close();
  }

  private void PrepareClosingOfDialog()
  {
    this.SaveDialogConfiguration();
    this.UnregisterFormEvents();
    this.UnregisterGeneralEvents();
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiECMEmphasizeContinuousMfgDlg));
    this.m_toolstrip = new TxToolStrip();
    this.m_widthMenu = new TxToolStripDropDownButton();
    this.m_colorMenu = new TxColorSelectToolStripButton();
    this.resetDefaultToolStripButton = new TxToolStripButton();
    this.label1 = new Label();
    ((Control) this.m_toolstrip).SuspendLayout();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_toolstrip, "m_toolstrip");
    this.m_toolstrip.ClickThrough = true;
    ((ToolStrip) this.m_toolstrip).GripStyle = ToolStripGripStyle.Hidden;
    ((ToolStrip) this.m_toolstrip).Items.AddRange(new ToolStripItem[3]
    {
      (ToolStripItem) this.m_widthMenu,
      (ToolStripItem) this.m_colorMenu,
      (ToolStripItem) this.resetDefaultToolStripButton
    });
    ((Control) this.m_toolstrip).Name = "m_toolstrip";
    ((ToolStripItem) this.m_widthMenu).DisplayStyle = ToolStripItemDisplayStyle.Image;
    componentResourceManager.ApplyResources((object) this.m_widthMenu, "m_widthMenu");
    ((ToolStripItem) this.m_widthMenu).Name = "m_widthMenu";
    ((ToolStripItem) this.m_widthMenu).Padding = new Padding(2);
    componentResourceManager.ApplyResources((object) this.m_colorMenu, "m_colorMenu");
    ((ToolStripItem) this.m_colorMenu).Name = "m_colorMenu";
    this.m_colorMenu.SelectedColor = Color.White;
    ((ToolStripItem) this.m_colorMenu).Click += new EventHandler(this.m_colorMenu_Click);
    ((ToolStripItem) this.resetDefaultToolStripButton).DisplayStyle = ToolStripItemDisplayStyle.Image;
    componentResourceManager.ApplyResources((object) this.resetDefaultToolStripButton, "resetDefaultToolStripButton");
    ((ToolStripItem) this.resetDefaultToolStripButton).Name = "resetDefaultToolStripButton";
    ((ToolStripItem) this.resetDefaultToolStripButton).Click += new EventHandler(this.resetDefaultToolStripButton_Click);
    componentResourceManager.ApplyResources((object) this.label1, "label1");
    this.label1.Name = "label1";
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Control) this).Controls.Add((Control) this.label1);
    ((Control) this).Controls.Add((Control) this.m_toolstrip);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedToolWindow;
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiECMEmphasizeContinuousMfgDlg);
    this.SemiModal = false;
    ((Form) this).ShowIcon = false;
    ((Form) this).SizeGripStyle = SizeGripStyle.Hide;
    ((Control) this.m_toolstrip).ResumeLayout(false);
    ((Control) this.m_toolstrip).PerformLayout();
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
